<?php

/**
 * install actions.
 *
 * @package    fsmartshop
 * @subpackage install
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class installActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $config = array();
      $config['appId'] = '400951353297880';
      $config['secret'] = '84a5b53b59646b254a4ad897fff86a37';
      $config['fileUpload'] = false; // optional

      $facebook = new Facebook($config);
      $uid = $facebook->getUser();
    
      if ($request->hasParameter('access_token') || $request->hasParameter('code')) {
          // Return from OAuth Dialog, App authorized
          $this->redirect(UrlFunctions::genFbUrl('@install_step0?auth=true', true));
      } elseif ($request->getParameter('error') === 'access_denied') {
          // Return from OAuth Dialog, App not authorized
          $this->redirect(UrlFunctions::genFbUrl('@install_step0?auth=false', true));
      } elseif ($request->hasParameter('fb_page_id')) {
          // Return from add.php, the App was just added to a page
          $url_params = LogInstall::log($request, 'step0_added_page');
          $this->redirect('@install_step1_save?'.http_build_query(array(
                      'page_id' => $request->getParameter('fb_page_id'),
                      'type'    => ($request->getParameter('fb_sig_type') === 'APPLICATION' ? 'app' : 'page'),
                      ), '', '&').'&'.$url_params);
      } elseif ($request->getParameter('auth') === 'false') {
          // Show auth explanation page
          $url_params = LogInstall::log($request, 'step0_auth_denied');
          return 'AccessDenied';
      } elseif ($request->getParameter('auth') === 'true' || $this->isAuthenticated()) {
          // Next step of the install process
          if ($request->getParameter('auth') !== 'true')
              LogInstall::log($request, 'step0'); // First access to step0
          $this->forward('install', 'step1');
      } else {
          LogInstall::log($request, 'step0'); // First access to step0
          $url_params = LogInstall::log($request, 'step0_auth_request');
          // Redirect to OAuth Dialog
          return 'AuthRequest';
      }
      return sfView::SUCCESS;
  }
}
